var $app = new Vue({
    el: '#app',
    data: {
        context: $contextPath || '',
        loading: false,
        loading4dig: false,
        level: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N'],
        rules: [
            {key: 'A', value: '90-100'},
            {key: 'B', value: '80-90'},
            {key: 'C', value: '70-80'},
            {key: 'D', value: '60-70'},
            {key: 'E', value: '0-60'}
        ],
        ruleResult : '',//最终生成的rule
        condition: {
            select: 'A',
            minRangeNum: 3
        },
        filePath: null,
        fileData: [],
        modal4view: false,
        filePath2: null,
        fileData2: [],
        modal4view2: false,
        //挖掘结果
        showResult: false,
        showResult2: false,
        //显示dig panel
        showDigPanel: true,
        showDigPanel2: true,
        digModel: {
            minSupport: 0.5,
            maxSupport: 0.8,
            peakValue: 0.9,
            minMetric: 0.8,
            delta: 0.01,
            subjectValue: 2,
            resultNum : 100
        },
        digResult: '',
        digResultTime: 0,
        digResult2: '',
        digResultTime2: 0,
        count4lift : 0,
        count4lift2 : 0,
        //插入成绩loading
        loading4save :false

    },
    created: function () {

    },
    methods: {
        //
        addRule: function () {
            var _this = this;
            _this.rules.push({key: '', value: ''});
        },
        //生成ARFF文件
        genFile: function (minRangeNum) {
            var _this = this;
            _this.modal4view = false;
            var pattern = /\d{1}(-){1}\d{1}/;
            var isValid = true;
            var rules = [];
            if (_this.rules.length > 0) {
                _this.rules.forEach(function (item, index) {
                    if (!item.value || !item.key || !pattern.test(item.value)) {
                        isValid = false;
                    } else {
                        rules.push(item.key + ':' + item.value)
                    }
                })
            } else {
                isValid = false;
            }
            if (!isValid) {
                _this.$message.warning('规则格式不正确，请检查..');
                return false;
            }
            _this.loading = true;
            _this.ruleResult = rules.join(',');
            $.ajax({
                url: _this.context + '/cal/genFile',
                method: 'POST',
                data: {
                    rule: _this.ruleResult,
                    minRangeNum: _this.condition.minRangeNum
                },
                success: function (res) {
                    _this.loading = false;
                    if (res) {
                        _this.filePath = res;
                    } else {
                        _this.filePath = null;
                        _this.$message.error('生成ARFF文件失败');
                    }
                },
                error: function () {
                    _this.loading = false;
                    _this.$message.warning('请求获取数据失败，请检查网络或登陆状态');
                }
            })
        },
        //view file
        view: function () {
            var _this = this;
            $.ajax({
                url: _this.context + '/cal/viewFile',
                method: 'POST',
                data: {filePath: _this.filePath},
                success: function (response) {
                    if (response) {
                        _this.fileData = response;
                    }
                    _this.modal4view = true;
                },
                error: function () {
                    _this.$message.warning('请求失败');
                }
            });
        },
        //挖掘数据
        digData: function () {
            var _this = this;
            if (_this.filePath) {
                _this.loading4dig = true;
                $.ajax({
                    url: _this.context + '/cal/digData',
                    method: 'POST',
                    data: {
                        filePath: _this.filePath,
                        delta: _this.digModel.delta,
                        minMetric: _this.digModel.minMetric,
                        minSupport: _this.digModel.minSupport,
                        maxSupport: _this.digModel.maxSupport,
                        peakValue: _this.digModel.peakValue,
                        subjectValue: _this.digModel.subjectValue,
                        resultNum : _this.digModel.resultNum,
                        isPro: true
                    },
                    success: function (response) {
                        if (response) {
                            _this.digResult = response.data;
                            _this.digResultTime = response.duration;
                            _this.count4lift = response.count4lift;
                        }
                        _this.loading4dig = false;
                    },
                    error: function () {
                        _this.$message.error('请求服务器失败');
                        _this.loading4dig = false;
                    }
                })
            }
        },

        //原算法
        //生成ARFF文件
        genFile2: function (minRangeNum) {
            var _this = this;
            _this.modal4view2 = false;
            var pattern = /\d{1}(-){1}\d{1}/;
            var isValid = true;
            var rules = [];
            if (_this.rules.length > 0) {
                _this.rules.forEach(function (item, index) {
                    if (!item.value || !item.key || !pattern.test(item.value)) {
                        isValid = false;
                    } else {
                        rules.push(item.key + ':' + item.value)
                    }
                })
            } else {
                isValid = false;
            }
            if (!isValid) {
                _this.$message.warning('规则格式不正确，请检查..');
                return false;
            }
            _this.loading = true;
            $.ajax({
                url: _this.context + '/cal/genFile',
                method: 'POST',
                data: {
                    rule: rules.join(','),
                    minRangeNum: 1
                },
                success: function (res) {
                    _this.loading = false;
                    if (res) {
                        _this.filePath2 = res;
                    } else {
                        _this.filePath2 = null;
                        _this.$message.error('生成ARFF文件失败');
                    }
                },
                error: function () {
                    _this.loading = false;
                    _this.$message.warning('请求获取数据失败，请检查网络或登陆状态');
                }
            })
        },
        //view file
        view2: function () {
            var _this = this;
            $.ajax({
                url: _this.context + '/cal/viewFile',
                method: 'POST',
                data: {filePath: _this.filePath2},
                success: function (response) {
                    if (response) {
                        _this.fileData2 = response;
                    }
                    _this.modal4view2 = true;
                },
                error: function () {
                    _this.$message.warning('请求失败');
                }
            });
        },
        //挖掘数据
        digData2: function () {
            var _this = this;
            if (_this.filePath) {
                _this.loading4dig = true;
                $.ajax({
                    url: _this.context + '/cal/digData',
                    method: 'POST',
                    data: {
                        filePath: _this.filePath2,
                        delta: _this.digModel.delta,
                        minMetric: _this.digModel.minMetric,
                        minSupport: _this.digModel.minSupport,
                        maxSupport: _this.digModel.maxSupport,
                        peakValue: _this.digModel.peakValue,
                        subjectValue: _this.digModel.subjectValue,
                        resultNum : _this.digModel.resultNum,
                        isPro: false
                    },
                    success: function (response) {
                        if (response) {
                            _this.digResult2 = response.data;
                            _this.digResultTime2 = response.duration;
                            _this.count4lift2 = response.count4lift;
                        }
                        _this.loading4dig = false;
                    },
                    error: function () {
                        _this.$message.error('请求服务器失败');
                        _this.loading4dig = false;
                    }
                })
            }
        },
        /**
         * 点击保存结果
         */
        saveData : function(){
            var _this = this;
            _this.loading4save = true;
            $.ajax({
                url : _this.context + '/cal/saveData',
                method : 'POST',
                data : {
                    rule : _this.ruleResult,
                    content : _this.digResult
                },
                success :function(response){
                    // console.log(response);
                    _this.loading4save = false;
                    _this.$message.success('保存关联结果成功');
                },
                error : function(){
                    _this.$message.info('请求失败');
                    _this.loading4save = false;
                }
            });
        }
    }
});